package com.wuba.rxjavademo.lyz.day0519;

import com.wuba.rxjavademo.lyz.BaseDemo;

import rx.Observable;
import rx.schedulers.Schedulers;

/**
 * Created by liyaxing on 2016/5/19.
 */
public class Demo02 extends BaseDemo {

    public static void main(String[] ss){
        print("thread 0 = " + Thread.currentThread().getId()) ;
        Observable
                .create(subscriber -> {
                    print("thread 1 = " + Thread.currentThread().getId());
                    subscriber.onNext(1);
                    subscriber.onCompleted();
                })
                .map(o -> {
                    print("thread 2 = " + Thread.currentThread().getId()) ;
                    return o ;
                })
                .doOnCompleted(() -> print("-----do doOnCompleted-----"))
                .observeOn(Schedulers.newThread())
                .map(o -> {
                    print("thread 3 = " + Thread.currentThread().getId()) ;
                    return o ;
                })
                .observeOn(Schedulers.newThread())
                .flatMap(o2 -> {
                    print("thread 4 = " + Thread.currentThread().getId()) ;
                    return Observable.create(subscriber -> {
                        print("thread 5 = " + Thread.currentThread().getId()) ;
                        subscriber.onNext(1);
                    }).subscribeOn(Schedulers.newThread()) ;
                })
                .doOnSubscribe(()-> print("thread 6 = " + Thread.currentThread().getId()))
                .doOnNext((i)-> print("thread 7 = " + Thread.currentThread().getId()))
                .observeOn(Schedulers.newThread())
                .subscribe(o1 -> {
                    print("thread 8 = " + Thread.currentThread().getId()) ;
                }) ;

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


}
